# 数据库链接
import sqlite3
import sys

from xjlibrary.mysqlmoudel.simple.mysqlclient import MySqlDbConnect, SelctSqlFromDB
from xjlibrary.tools.BaseFile import BaseFile
from xjlibrary.tools.BaseUrl import BaseUrl

curPath = BaseFile.get_path_absolute(__file__)

def MajorDbConnect():
    return MySqlDbConnect(curPath, "db.ini")


def InsertSqlToDB3(sql,listfaileds):
    try:
        conn = sqlite3.connect('mirrordqlibtingbookvideo.db3')
        cur = conn.cursor()
        row = cur.executemany(sql,listfaileds)
        # if row.rowcount != 1:
        #     print(sql)
        #     print("请检查  按照逻辑应该不存在这种情况")
        #     sys.exit(-1)
        conn.commit()
        cur.close()
        conn.close()
    except Exception as e:
        raise e

def SelectFromDB():
    sql = "select `url`,`imageurl` from `chapter`"
    conn = MajorDbConnect()
    rows = SelctSqlFromDB(sql, conn)
    return rows

if __name__=="__main__":
    dicts = {}
    listfaileds=[]
    sql = "update `modify_title_info_zt` set cover=? where rawid=?"
    count = 0
    provider = "mirrordqlibtingbookvideo"
    for row in SelectFromDB():
        id = BaseUrl.urlQuery2Dict(row[0])["id"]
        listurl = BaseUrl.urlPath2List(row[1])[-2:]
        image_file_name = "_".join(listurl).replace("_picture", "").replace(".gif", ".jpg")
        cover = '/smartlib/' + provider + '/' + image_file_name
        dicts[id] = cover
        listfaileds.append((str(cover), str(id)))
        if len(listfaileds) > 1000:
            InsertSqlToDB3(sql, listfaileds)
            listfaileds = []
            count += 1000
            print(count)
    print(len(listfaileds))
    count += len(listfaileds)
    InsertSqlToDB3(sql, listfaileds)
    listfaileds=[]
    print(count)